﻿using Advantech.Motion;
using APS_Define_W32;
using APS168_W32;
using AutoMachine;
using CommonTool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tool;

namespace Motions
{

    public class MotionMgr : SingletonTemplate<MotionMgr>
    {

        public Motions Card = new AdvantechMotion(0, "研华板卡", 0, 31);

        /// <summary>
        /// 初始化所有轴卡
        /// </summary>
        /// <returns></returns>
        public void InitMotionCard()
        {
            Card.InitMotionCard();
        }

        /// <summary>
        /// 反初始化所有轴卡
        /// </summary>
        public void DeinitMotionCard()
        {

            Card.DeInitMotionCard();

        }

        /// <summary>
        /// 扩展IO卡0 初始化
        /// </summary>
        /// <returns></returns>
        public void InitExtIoCard()
        {
            Card.InitExtIoCard(0);

        }

        /// <summary>
        /// 反初始化扩展IO卡0
        /// </summary>
        public void DeInitExtIoCard()
        {
            Card.DeInitExtIoCard(0);

        }

        /// <summary>
        /// 所以轴上使能
        /// </summary>
        public void ALLSeverOn()
        {
            foreach (SystemCfg.AxisNameInfo item in SystemCfg.AxisName.Values)
            {
                Card.SetPosZero(item.Index);
                    Card.ServoOn(item.Index);
                    Thread.Sleep(100);
                
              
            }
        }

        /// <summary>
        /// 所有轴下使能
        /// </summary>
        public void ALLSeverOff()
        {
            foreach (SystemCfg.AxisNameInfo item in SystemCfg.AxisName.Values)
            {
                if (item.Index > 0)
                {
                    Card.ServoOff(item.Index);
                    Thread.Sleep(100);
                }
            }
        }

        /// <summary>
        /// 所有轴停止
        /// </summary>
        public void ALLStopAxis()
        {
            foreach (SystemCfg.AxisNameInfo item in SystemCfg.AxisName.Values)
            {
                if (item.Index > 0)
                {
                    Card.StopAxis(item.Index);
                    Thread.Sleep(100);
                }
            }
        }

        /// <summary>
        /// 所有轴紧急停止
        /// </summary>
        public void ALLEmgAxis()
        {
            foreach (SystemCfg.AxisNameInfo item in SystemCfg.AxisName.Values)
            {
                if (item.Index > 0)
                {
                    Card.EmgAxis(item.Index);
                    Thread.Sleep(100);
                }
            }
        }

        /// <summary>
        /// 任意轴报警
        /// </summary>
        /// <returns></returns>
        public bool AxisAlarm()
        {
            foreach (SystemCfg.AxisNameInfo item in SystemCfg.AxisName.Values)
            {
                if (item.Index > 0)
                {
                    if (Card.GetAxisAlarm(item.Index) == true)
                    {
                        LogView.GetInstance().ShowLog(LogView.LogType.Error, item.ToString() + item.Name + "轴报警");
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 任意正限位
        /// </summary>
        /// <returns></returns>
        public bool AxisPosLimit()
        {
            foreach (SystemCfg.AxisNameInfo item in SystemCfg.AxisName.Values)
            {
                if (item.Index > 0)
                {
                    if (Card.GetPosLimit(item.Index) == true)
                    {
                        Thread.Sleep(400);
                        if (Card.GetPosLimit(item.Index) == true)
                        {
                            LogView.GetInstance().ShowLog(LogView.LogType.Error, item.ToString() + item.Name + "轴正限位触发");
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 任意负限位
        /// </summary>
        /// <returns></returns>
        public bool AxisNegLimit()
        {
            foreach (SystemCfg.AxisNameInfo item in SystemCfg.AxisName.Values)
            {
                if (item.Index > 0)
                {
                    if (Card.GetNegLimit(item.Index) == true)
                    {
                        Thread.Sleep(400);
                        if (Card.GetNegLimit(item.Index) == true)
                        {
                            LogView.GetInstance().ShowLog(LogView.LogType.Error, item.ToString() + item.Name + "轴负限位触发");
                            return true;
                        }

                    }
                }
            }
            return false;
        }


        /// <summary>
        /// 绝对定位
        /// </summary>
        /// <param name="SectionName">XML集合段落名称</param>
        /// <param name="PointName">点位名称</param>
        public void JogMove(string SectionName, string PointName, bool Dir)
        {
            int nAxisNo = SystemCfg.Po(SectionName)[PointName].AxisNo;//轴号
            double Speed = SystemCfg.Po(SectionName)[PointName].Speed;//速度    
            
            //绝对定位
            Card.JogMove(nAxisNo,Speed,Dir);
        }

        /// <summary>
        /// 绝对定位
        /// </summary>
        /// <param name="SectionName">XML集合段落名称</param>
        /// <param name="PointName">点位名称</param>
        public void AbsMove(string SectionName,string PointName, double offset=0)
        {
            int nAxisNo = SystemCfg.Po(SectionName)[PointName].AxisNo;//轴号
            double Speed = SystemCfg.Po(SectionName)[PointName].Speed;//速度 
            double Pos = SystemCfg.Po(SectionName)[PointName].Point+ offset;//目标位置    
            //绝对定位
            Card.AbsMove(nAxisNo, Pos, Speed);
        }

        /// <summary>
        /// 等待轴到位
        /// </summary>
        /// <param name="AxisName"></param>
        public void WaitAbs(string SectionName,string PointName,double offset=0)
        {
        Line1:
            DateTime now = DateTime.Now;
            while (true)
            {
                int nAxisNo = SystemCfg.Po(SectionName)[PointName].AxisNo;//轴号
                double PosValue = SystemCfg.Po(SectionName)[PointName].Point+ offset;//目标位置         
                double PosActValue = Card.GetAxisActPos(nAxisNo);//实际位置
                double DisPos = PosValue - PosActValue;//目标位置与实际位置的差值
                bool a = Card.GetAxisDone(nAxisNo);//到位完成信号  
                bool b = (DisPos >= -2) && (DisPos <= 2);//目标位置与实际位置的差值在-20到20之间
                bool c = Card.GetAxisBusy(nAxisNo);//轴Busy信号
                if (a == true && b == true && c == false) //轴到位 位置相差50 轴不忙
                {
                    LogView.GetInstance().ShowLog(LogView.LogType.Oper, PointName + "定位到位");
                    break;
                }
                else
                {
                    Thread.Sleep(10);
                    if ((DateTime.Now - now).Seconds > Convert.ToInt16(SystemCfg.Param["MotionWaitTime"].Value.ToString()))//2分钟
                    {
                        LogView.GetInstance().ShowLog(LogView.LogType.Error, PointName + "到位超时");
                        GlobalVar.Buzzer = true;//蜂鸣器
                        DialogResult result = MessageBox.Show(PointName + "  " + "轴到位", "超时提醒", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);//最后一个改变默认焦点
                        if (result == DialogResult.Abort)
                        {
                            if (Device.StateMode == Device.STATEMode.Resetting)
                            {
                                Device.ChangeStateMode(Device.STATEMode.STOP);//切换暂停
                                break;
                            }
                            if (Device.StateMode == Device.STATEMode.RUN)
                            {
                                Device.ChangeStateMode(Device.STATEMode.PAUSE);//切换暂停
                                break;
                            }
                            GlobalVar.Buzzer = false;
                            break;
                        }
                        else if (result == DialogResult.Retry)
                        {
                            GlobalVar.Buzzer = false;
                            //重试
                            goto Line1;//重置超时
                        }
                        else if (result == DialogResult.Ignore)
                        {
                            GlobalVar.Buzzer = false;
                            //忽略
                            break;
                        }

                    }
                }
                Thread.Sleep(5);
            }
        }

        /// <summary>
        /// 绝对定位
        /// </summary>
        /// <param name="SectionName"></param>
        /// <param name="PointName"></param>
        public void AbsMove(int nAxisNo, double Pos, double Speed)
        {
            //int nAxisNo = SystemCfg.Po(SectionName)[PointName].AxisNo;//轴号
            //double Speed = SystemCfg.Po(SectionName)[PointName].Speed;//速度 
            //double Pos = SystemCfg.Po(SectionName)[PointName].Point;//目标位置    
            //绝对定位
            Card.AbsMove(nAxisNo, Pos, Speed);
        }

        /// <summary>
        /// 等待轴到位
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <param name="Pos"></param>
        public void WaitAbs(int nAxisNo, double Pos)
        {
            Line1:
            DateTime now = DateTime.Now;
            while (true)
            {
                double PosValue = Pos;//位置                
                double PosActValue = Card.GetAxisActPos(nAxisNo);//实际位置
                double DisPos = PosValue - PosActValue;//目标位置与实际位置的差值
                bool a = Card.GetAxisDone(nAxisNo);//到位完成信号  
                bool b = (DisPos >= -0.7) && (DisPos <= 0.7);//目标位置与实际位置的差值在-20到20之间
                bool c = Card.GetAxisBusy(nAxisNo);//轴Busy信号
                if (a == true && b == true && c == false) //轴到位 位置相差50 轴不忙
                {
                    //LogView.GetInstance().ShowLog(LogView.LogType.Oper, AxisName + "定位到位");
                    break;
                }
                else
                {
                    Thread.Sleep(10);
                    if ((DateTime.Now - now).Seconds > Convert.ToInt16(SystemCfg.Param["MotionWaitTime"].Value.ToString()))//2分钟
                    {
                        LogView.GetInstance().ShowLog(LogView.LogType.Error, nAxisNo + "到位超时");
                        GlobalVar.Buzzer = true;//蜂鸣器
                        DialogResult result = MessageBox.Show(nAxisNo + "  " + "轴到位", "超时提醒", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);//最后一个改变默认焦点
                        if (result == DialogResult.Abort)
                        {
                            if (Device.StateMode == Device.STATEMode.Resetting)
                            {
                                Device.ChangeStateMode(Device.STATEMode.STOP);//切换暂停
                                break;
                            }
                            if (Device.StateMode == Device.STATEMode.RUN)
                            {
                                Device.ChangeStateMode(Device.STATEMode.PAUSE);//切换暂停
                                break;
                            }
                            GlobalVar.Buzzer = false;//蜂鸣器
                            break;
                        }
                        else if (result == DialogResult.Retry)
                        {
                            GlobalVar.Buzzer = false;//蜂鸣器
                            Card.AbsMove(nAxisNo, Pos, 100);
                            //重试
                            goto Line1;//重置超时
                        }
                        else if (result == DialogResult.Ignore)
                        {
                            GlobalVar.Buzzer = false;//蜂鸣器
                            //忽略
                            break;
                        }
                    }
                }
                Thread.Sleep(5);
            }
        }

        /// <summary>
        /// 轴回原
        /// </summary>
        /// <param name="AxisName"></param>
        public void HomeMove(string AxisName)
        {
            int nAxisNo = SystemCfg.AxisName[AxisName].Index; //轴号
            int MoveDir = SystemCfg.AxisCfg[nAxisNo].HomeDir; //回原方向
            uint HomeMode =SystemCfg.AxisCfg[nAxisNo].HomeMode;
            //回原运动
            Card.HomeMove(nAxisNo, MoveDir, HomeMode);
        }

        /// <summary>
        /// 轴回限位，MoveDir 0是正极限，1是负极限
        /// </summary>
        /// <param name="AxisName"></param>
        public void HomeLimt(string AxisName, int MoveDir)
        {
            int nAxisNo = SystemCfg.AxisName[AxisName].Index; //轴号
            //研华板卡1是回极限
            uint HomeMode = 1;
            //回原运动
            Card.HomeMove(nAxisNo, MoveDir, HomeMode);
        }


        /// <summary>
        /// 轴撞极限,0是正方向，1是负方向
        /// </summary>
        /// <param name="AxisName"></param>
        public void HomeMoveORG(string AxisName,int Mdir)
        {
            int nAxisNo = SystemCfg.AxisName[AxisName].Index; //轴号
            int MoveDir = SystemCfg.AxisCfg[nAxisNo].HomeDir; //回原方向
            uint HomeMode = SystemCfg.AxisCfg[nAxisNo].HomeMode;
            //0是正方向，1是负方向
            if (Mdir==0)
            {
                HomeMode = 10;
            }
            else
            {
                HomeMode = 10;
            }
            //回原运动
            Card.HomeMove(nAxisNo, MoveDir, HomeMode);
        }

        /// <summary>
        /// 等待轴回原点结束
        /// </summary>
        /// <param name="AxisName"></param>
        public void WaitHome(string AxisName)
        {
            Line1:
            DateTime now = DateTime.Now;
            while (true)
            {
                int nAxisNo = SystemCfg.AxisName[AxisName].Index;//轴号
                double PosActValue = Card.GetAxisActPos(nAxisNo);//实际位置
                bool a = Card.GetAxisDone(nAxisNo);//到位完成信号            
                bool b = PosActValue <= 5;//当前位置为0
                //bool c = Card.GetAxisReady(nAxisNo);//Ready信号        
                bool d = Card.HomeOK(nAxisNo);//原点感应器信号
                bool e = Card.GetAxisBusy(nAxisNo);//Busy信号
                if (a && b && !e && d)
                {
                    Thread.Sleep(500);
                    LogView.GetInstance().ShowLog(LogView.LogType.Oper, AxisName + "回原到位");
                    break;
                }
                else
                {
                    Thread.Sleep(10);
                    if ((DateTime.Now - now).Seconds > Convert.ToInt16(SystemCfg.Param["MotionWaitTime"].Value.ToString()))//2分钟
                    {
                        LogView.GetInstance().ShowLog(LogView.LogType.Error, AxisName + "回原超时");
                        GlobalVar.Buzzer = true;//蜂鸣器
                        DialogResult result = MessageBox.Show(AxisName + "  " + "轴回原", "超时提醒", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                        if (result == DialogResult.Abort)
                        {
                            if (Device.StateMode == Device.STATEMode.Resetting)
                            {
                                Device.ChangeStateMode(Device.STATEMode.STOP);//切换暂停
                                break;
                            }
                            if (Device.StateMode == Device.STATEMode.RUN)
                            {
                                Device.ChangeStateMode(Device.STATEMode.PAUSE);//切换暂停
                                break;
                            }
                            GlobalVar.Buzzer = false;//蜂鸣器
                            break;
                        }
                        else if (result == DialogResult.Retry)
                        {
                            GlobalVar.Buzzer = false;//蜂鸣器
                            //重试
                            goto Line1;//重置超时
                        }
                        else if (result == DialogResult.Ignore)
                        {
                            GlobalVar.Buzzer = false;//蜂鸣器
                            //忽略
                            break;
                        }
                    }
                }
                Thread.Sleep(5);
            }

        }

        /// <summary>
        /// 等待轴回极限结束
        /// </summary>
        /// <param name="AxisName"></param>
        public void WaitLimt(string AxisName,int Mdir)
        {
        Line1:
            DateTime now = DateTime.Now;
            while (true)
            {
                int nAxisNo = SystemCfg.AxisName[AxisName].Index;//轴号
                double PosActValue = Card.GetAxisActPos(nAxisNo);//实际位置
                bool a = Card.GetNegLimit(nAxisNo);//            
                if (Mdir==0)
                {
                    a = Card.GetPosLimit(nAxisNo);//   
                }
                         
                bool b = PosActValue <= 5;//当前位置为0
                //bool c = Card.GetAxisReady(nAxisNo);//Ready信号        
               
                bool e = Card.GetAxisBusy(nAxisNo);//Busy信号
                if (a && b && !e)
                {
                    Thread.Sleep(500);
                    LogView.GetInstance().ShowLog(LogView.LogType.Oper, AxisName + "回原极限位");
                    break;
                }
                else
                {
                    Thread.Sleep(10);
                    if ((DateTime.Now - now).Seconds > Convert.ToInt16(SystemCfg.Param["MotionWaitTime"].Value.ToString()))//2分钟
                    {
                        LogView.GetInstance().ShowLog(LogView.LogType.Error, AxisName + "回原超时");
                        GlobalVar.Buzzer = true;//蜂鸣器
                        DialogResult result = MessageBox.Show(AxisName + "  " + "轴回原", "超时提醒", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                        if (result == DialogResult.Abort)
                        {
                            if (Device.StateMode == Device.STATEMode.Resetting)
                            {
                                Device.ChangeStateMode(Device.STATEMode.STOP);//切换暂停
                                break;
                            }
                            if (Device.StateMode == Device.STATEMode.RUN)
                            {
                                Device.ChangeStateMode(Device.STATEMode.PAUSE);//切换暂停
                                break;
                            }
                            GlobalVar.Buzzer = false;//蜂鸣器
                            break;
                        }
                        else if (result == DialogResult.Retry)
                        {
                            GlobalVar.Buzzer = false;//蜂鸣器
                            //重试
                            goto Line1;//重置超时
                        }
                        else if (result == DialogResult.Ignore)
                        {
                            GlobalVar.Buzzer = false;//蜂鸣器
                            //忽略
                            break;
                        }
                    }
                }
                Thread.Sleep(5);
            }

        }
        public void WaitHome(int nAxisNo)
        {
        Line1:
            DateTime now = DateTime.Now;
            while (true)
            {
               
                double PosActValue = Card.GetAxisActPos(nAxisNo);//实际位置
                bool a = Card.GetAxisDone(nAxisNo);//到位完成信号            
                bool b = PosActValue <= 3;//当前位置为0
                bool c = Card.GetAxisReady(nAxisNo);//Ready信号        
                bool d = Card.HomeOK(nAxisNo);//原点感应器信号
                bool e = Card.GetAxisBusy(nAxisNo);//Busy信号
                if (a == true && b == true && e == false)
                {
                    Thread.Sleep(500);
                    LogView.GetInstance().ShowLog(LogView.LogType.Oper, nAxisNo + "轴回原到位");
                    break;
                }
                else
                {
                    Thread.Sleep(10);
                    if ((DateTime.Now - now).Seconds > Convert.ToInt16(SystemCfg.Param["MotionWaitTime"].Value.ToString()))//2分钟
                    {
                        LogView.GetInstance().ShowLog(LogView.LogType.Error, nAxisNo + "轴回原超时");
                        GlobalVar.Buzzer = true;//蜂鸣器
                        DialogResult result = MessageBox.Show(nAxisNo + "  " + "轴回原", "超时提醒", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                        if (result == DialogResult.Abort)
                        {
                            if (Device.StateMode == Device.STATEMode.Resetting)
                            {
                                Device.ChangeStateMode(Device.STATEMode.STOP);//切换暂停
                                break;
                            }
                            if (Device.StateMode == Device.STATEMode.RUN)
                            {
                                Device.ChangeStateMode(Device.STATEMode.PAUSE);//切换暂停
                                break;
                            }
                            GlobalVar.Buzzer = false;//蜂鸣器
                            break;
                        }
                        else if (result == DialogResult.Retry)
                        {
                            GlobalVar.Buzzer = false;//蜂鸣器
                            //重试
                            goto Line1;//重置超时
                        }
                        else if (result == DialogResult.Ignore)
                        {
                            GlobalVar.Buzzer = false;//蜂鸣器
                            //忽略
                            break;
                        }
                    }
                }
                Thread.Sleep(5);
            }

        }
        /// <summary>
        /// 输出信号
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Bit"></param>
        public void WriteOut(string name, bool Bit)
        {
            if (Card.CardIniOK)
            {
                int nCardN0 = SystemCfg.Out[name].CardN0;
                int nIndex = SystemCfg.Out[name].Index;
                int Axisfg = SystemCfg.Out[name].Axisfg;
                if (Axisfg != 0)
                {

                    Card.WriteExtOut(nCardN0, nIndex, Bit);
                }
                else
                {
                    Card.WriteOut(nCardN0, nIndex, Bit);
                }
            }
            

        }

        /// <summary>
        /// 等待输入信号
        /// </summary>
        /// <param name="name"></param>
        /// <param name="bit"></param>
        public void WaitIn(string name, bool bit)
        {
            
            //   LogView.GetInstance().ShowLog(LogView.LogType.Oper, "等待" + name + " " + Convert.ToString(bit));
            Line1:
            DateTime now = DateTime.Now;
            if (Card.CardIniOK)
            {
                
                while (true)
                {
                    int nCardN0 = SystemCfg.In[name].CardN0;
                    int nIndex = SystemCfg.In[name].Index;
                    bool a;
                    int Axisfg = SystemCfg.In[name].Axisfg;
                    if (Axisfg != 0)
                    {
                        a = GlobalVar.GetDI[nIndex];
                      //  a = Card.ReadExtIn(nCardN0, nIndex);
                    }
                    else
                    {
                        a = GlobalVar.GetDI[nIndex];
                        //   a = Card.ReadIn(nCardN0, nIndex);
                    }
                    if (a == bit)
                    {
                        break;
                    }
                    else
                    {
                        Thread.Sleep(5);
                        if ((DateTime.Now - now).Seconds > Convert.ToInt16(SystemCfg.Param["IoWaitTime"].Value.ToString()))//10秒
                        {
                            LogView.GetInstance().ShowLog(LogView.LogType.Error, name + "信号超时" + bit);
                            GlobalVar.Buzzer = true;//蜂鸣器
                            DialogResult result = MessageBox.Show(name + "  " + "位信号", "超时提醒", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                            if (result == DialogResult.Abort)
                            {
                                if (Device.StateMode == Device.STATEMode.Resetting)
                                {
                                    Device.ChangeStateMode(Device.STATEMode.STOP);//切换暂停
                                    break;
                                }
                                if (Device.StateMode == Device.STATEMode.RUN)
                                {
                                    Device.ChangeStateMode(Device.STATEMode.PAUSE);//切换暂停
                                    break;
                                }
                                GlobalVar.Buzzer = false;//蜂鸣器
                                break;
                            }
                            else if (result == DialogResult.Retry)
                            {
                                GlobalVar.Buzzer = false;//蜂鸣器
                                                         //重试
                                goto Line1;//重置超时
                            }
                            else if (result == DialogResult.Ignore)
                            {
                                GlobalVar.Buzzer = false;//蜂鸣器
                                                         //忽略
                                break;
                            }
                        }
                    }
                    Thread.Sleep(5);
                }
            }




        }

        /// <summary>
        /// 等到输出信号
        /// </summary>
        /// <param name="name"></param>
        /// <param name="bit"></param>
        public void WaitOut(string name, bool bit)  
        {
            //   LogView.GetInstance().ShowLog(LogView.LogType.Oper, "等待" + name + " " + Convert.ToString(bit));
            Line1:
            DateTime now = DateTime.Now;
            while (true)
            {
                
                int nCardN0 = SystemCfg.Out[name].CardN0;
                int nIndex = SystemCfg.Out[name].Index;
                int Axisfg = SystemCfg.Out[name].Axisfg;
                bool a;
                if (Axisfg != 0)
                {
                    a = Card.ReadExtOut(nCardN0, nIndex);
                }
                else
                {
                    a = Card.ReadOut(nCardN0,nIndex);
                }
                if (a == bit)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(10);
                    if ((DateTime.Now - now).Seconds > Convert.ToInt16(SystemCfg.Param["IoWaitTime"].Value.ToString()))//1秒
                    {
                        LogView.GetInstance().ShowLog(LogView.LogType.Error, name + "信号超时" + bit);
                        GlobalVar.Buzzer = true;//蜂鸣器
                        DialogResult result = MessageBox.Show(name + "  " + "位信号", "超时提醒", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                        if (result == DialogResult.Abort)
                        {
                            if (Device.StateMode == Device.STATEMode.Resetting)
                            {
                                Device.ChangeStateMode(Device.STATEMode.STOP);//切换暂停
                                break;
                            }
                            if (Device.StateMode == Device.STATEMode.RUN)
                            {
                                Device.ChangeStateMode(Device.STATEMode.PAUSE);//切换暂停
                                break;
                            }
                            GlobalVar.Buzzer = false;//蜂鸣器
                            break;
                        }
                        else if (result == DialogResult.Retry)
                        {
                            GlobalVar.Buzzer = false;//蜂鸣器
                            //重试
                            goto Line1;//重置超时
                        }
                        else if (result == DialogResult.Ignore)
                        {
                            GlobalVar.Buzzer = false;//蜂鸣器
                            //忽略
                            break;
                        }
                    }
                }
                Thread.Sleep(5);
            }



        }
        public bool ReadIn(string name)
        {
            int nCardN0 = SystemCfg.In[name].CardN0;
            int nIndex = SystemCfg.In[name].Index;
            int Axisfg = SystemCfg.In[name].Axisfg;
            bool a;
            if (Axisfg != 0)
            {
                a = GlobalVar.GetDI[nIndex];
            }
            else
            {
                a = GlobalVar.GetDI[nIndex];
            }
            if (a==true)
            {
                return true;
            }
            return false;
        }
        public bool ReadOut(string name)
        {
            int nCardN0 = SystemCfg.Out[name].CardN0;
            int nIndex = SystemCfg.Out[name].Index;
            int Axisfg = SystemCfg.Out[name].Axisfg;
            bool a;
            if (Axisfg != 0)
            {
                a = Card.ReadExtOut(nCardN0, nIndex);
            }
            else
            {
                a = Card.ReadOut(nCardN0, nIndex);
            }
            if (a == true)
            {
                return true;
            }
            return false;
        }

        #region 位置比较输出
        /// <summary>
        /// 打开位置比较功能 卡号，通道号，方向（正向0负向1）
        /// </summary>
        /// <param name="cardNum"></param>
        /// <param name="channel"></param>
        /// <param name="dir"></param>
        public void OpenCmd(int cardNum, ushort channel, uint dir)
        {
            Card.OpenCmd(cardNum, channel, dir);
        }
        /// <summary>
        /// 关闭位置比较功能
        /// </summary>
        /// <param name="cardNum"></param>
        /// <param name="channel"></param>
        public void CloseCmd(int cardNum, ushort channel)
        {
          Card.CloseCmd(cardNum, channel);
        }
        /// <summary>
        /// 设置位置比较数据  卡号，通道号，飞拍点位（实际点位*脉冲当量）
        /// </summary>
        /// <param name="cardNum"></param>
        /// <param name="channel"></param>
        /// <param name="data"></param>
        public void SetCmdData(int cardNum, ushort channel, string data)
        {
            string[] str = data.Split(',');
            double[] data1 = new double[str.Count()];
            for (int i = 0; i < str.Count(); i++)
            {
                data1[i] = double.Parse(str[i])*1000;
            }
          Card.SetCmdData(cardNum, channel, data1);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cardNum"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public int GetCmdFIFOCount(int cardNum, ushort channel)
        {
            return Card.GetCmdFIFOCount(cardNum, channel);
        }
        /// <summary>
        /// 打开位置比较相关输出
        /// </summary>
        /// <param name="cardNum"></param>
        /// <param name="channel"></param>
        public void SetCmdDO(int cardNum, ushort channel)
        {
          Card.SetCmdDO(cardNum, channel);
        }
        /// <summary>
        /// 关闭位置比较相关输出
        /// </summary>
        /// <param name="cardNum"></param>
        /// <param name="channel"></param>
        public void CloseCmdDO(int cardNum, ushort channel)
        {
            Card.CloseCmdDO(cardNum, channel);
        }
        /// <summary>
        /// 重置指定比较通道的内容
        /// </summary>
        /// <param name="cardNum"></param>
        /// <param name="channel"></param>
        public void ResetCmd(int cardNum, ushort channel)
        {
           Card.ResetCmd(cardNum, channel);
        }

        #endregion

    }
}
